using Microsoft.EntityFrameworkCore;
using IRepositories;
namespace Repositories
{
	public partial class UnitOfWork:IUnitOfWork
	{
		private readonly DbContext dbContext;
		
		public UnitOfWork(DbContext dbContext)
		{
			this.dbContext = dbContext;
		}

        public DbContext GetDbContext()
        {
            return this.dbContext;
        }

        public void BeginTrasaction()
        {
            lock (this)
            {
                dbContext.Database.BeginTransaction();
            }
        }

        public void CommitTrasaction()
        {
            try
            {
                dbContext.Database.CommitTransaction();
            }
            catch (Exception)
            {
                dbContext.Database.RollbackTransaction();
                throw;
            }
        }

        public void RollbackTransaction()
        {
            lock (this)
            {
                dbContext.Database.RollbackTransaction();
            }
        }
	}
}
